home *** CD-ROM | disk | FTP | other *** search
/ MACD 5 / MACD 5.bin / workbench / monitory / sysmon / src / startsm.c < prev    next >
C/C++ Source or Header  |  1995-11-20  |  8KB  |  284 lines

  1. /*
  2. **    $RCSfile: StartSM.c,v $
  3. **    $Filename: StartSM.c $
  4. **    $Revision: 0.6 $
  5. **    $Date: 1995/11/04 14:11:26 $
  6. **
  7. **    sysmon.library startup command StartSM (version 0.7) 
  8. **    
  9. **    (C) Copyright 1995 by Etienne Vogt
  10. */
  11.  
  12. #include <exec/alerts.h>
  13. #include <exec/memory.h>
  14. #include <exec/execbase.h>
  15. #include <dos/dos.h>
  16. #include <dos/dostags.h>
  17. #include <workbench/startup.h>
  18. #define __USE_SYSBASE
  19. #include <proto/exec.h>
  20. #include <proto/dos.h>
  21. #include "sysmon.h"
  22. #include "sysmon_protos.h"
  23. #include "sysmon_pragmas.h"
  24. #include <string.h>
  25. #include <stdlib.h>
  26. #include <ctype.h>
  27.  
  28. struct ExecBase *SysBase;
  29. struct DosLibrary *DOSBase;
  30. struct SysmonBase *SysmonBase;
  31. static struct WBStartup *wbmsg;
  32. static struct RDArgs *myrda;
  33.  
  34. static UBYTE version[] = "$VER: StartSM 0.7 (19.11.95)";
  35. static UBYTE template[] = "CONF=CONFIGFILE";
  36. static UBYTE configfile[] = "S:Sysmon.config";
  37. static UBYTE syslogfile[] = "S:Sysmon.log";
  38. static UBYTE syslogwindow[] = "CON:20/50/600/80/SysLog/AUTO/CLOSE/WAIT";
  39. static ULONG filepri = LOG_DEBUG;
  40. static ULONG windowpri = LOG_WARN;
  41. static ULONG consolepri = LOG_EMERG;
  42. static ULONG numbuffers = 5;
  43. static ULONG stampperiod = 60;    /* 1 hour default period    */
  44. static int idleled = FALSE;
  45. static STRPTR logfile,logwindow;
  46.  
  47. #define    OPT_CONFIG    0
  48. #define OPTMAX        1
  49.  
  50. /* Definitions for config file parser    */
  51.  
  52. #define P_IGNORE    0
  53. #define P_STRING    1
  54. #define P_BOOL        2
  55. #define P_INT        3
  56.  
  57. #define    OK        0
  58. #define    ERR_OPEN    1
  59. #define ERR_UNKNPREF    2
  60. #define ERR_NOMEM    3
  61.  
  62. static STRPTR errmsg[] = {
  63.     "OK",
  64.     "Unable to Open",
  65.     "Unknown Keyword",
  66.     "Out of memory",
  67. };
  68.  
  69. typedef struct
  70. {    STRPTR    name;            /* Configuration item name    */
  71.     APTR    pflag;            /* Pointer to variable    */
  72.     UWORD    nbmin_char;        /* Required characters in item name    */
  73.     UWORD    type;            /* Parameter type    */
  74. } Pref;
  75.  
  76. Pref Prefs[] = {
  77.     {"LOGFILE",        &logfile,    4,    P_STRING},
  78.     {"LOGWINDOW",        &logwindow,    4,    P_STRING},
  79.     {"FILEPRI",        &filepri,    5,    P_INT},
  80.     {"WINPRI",        &windowpri,    4,    P_INT},
  81.     {"CONPRI",        &consolepri,    4,    P_INT},
  82.     {"LOGBUFFERS",        &numbuffers,    4,    P_INT},
  83.     {"STAMPPERIOD",        &stampperiod,    6,    P_INT},
  84.     {"IDLELED",        &idleled,    4,    P_BOOL},
  85.     {"#",            NULL,        1,    P_IGNORE},
  86.     {";",            NULL,        1,    P_IGNORE},
  87. };
  88.  
  89. #define NB_PREFS (sizeof(Prefs)/sizeof(Pref))
  90.  
  91. ULONG __saveds main(void);
  92. static void cleanexit(ULONG rc);
  93. static void KPrintf(STRPTR fmt, ...);
  94. static BOOL SysLog(ULONG priority, STRPTR format, ...);
  95. static int ReadConfig(STRPTR file);
  96. static char *strtokg(char *s1, char *s2);
  97. static int InitPrefLine(char *line, Pref *pref_array, int nb_prefs);
  98.  
  99. ULONG __saveds main(void)    /* No startup code */
  100. {
  101.   struct Process *myproc;
  102.   LONG opts[OPTMAX];
  103.   int error;
  104.  
  105.   SysBase = *(struct ExecBase **)4;
  106.   DOSBase = NULL;
  107.   SysmonBase = NULL;
  108.   wbmsg = NULL;
  109.   myrda = NULL;
  110.  
  111.   myproc = (struct Process *)FindTask(NULL);
  112.   if ((DOSBase = (struct DosLibrary *)OpenLibrary("dos.library",36)) == NULL)
  113.   { Alert(AT_Recovery|AG_OpenLib|AO_DOSLib);
  114.     return 100;
  115.   }
  116.  
  117.   if (!(myproc->pr_CLI))        /* If started from WB, exit cleanly */
  118.   { WaitPort(&(myproc->pr_MsgPort));
  119.     wbmsg = (struct WBStartup *)GetMsg(&(myproc->pr_MsgPort));
  120.     cleanexit(20);
  121.   }
  122.   else
  123.   { if ((SysmonBase = (struct SysmonBase *)OpenLibrary("sysmon.library",0)) == NULL)
  124.     { Printf("StartSM : Couldn't open sysmon.library\n");
  125.       cleanexit(20);
  126.     }
  127.     memset((char *)opts, 0, sizeof(opts));
  128.     if ((myrda = ReadArgs(template, opts, NULL)) == NULL)
  129.     { PrintFault(IoErr(),NULL);
  130.       cleanexit(20);
  131.     }
  132.     if (opts[OPT_CONFIG] == 0) opts[OPT_CONFIG] = (LONG)configfile;
  133.  
  134.     Forbid();
  135.     if (SysmonBase->sb_SyslogPort)
  136.     { Permit();
  137.       Printf("StartSM : The %s process is already running.\n", SysmonBase->sb_ServerName);
  138.       cleanexit(10);
  139.     }
  140.     Permit();
  141.     if ((logfile = AllocVec(strlen(syslogfile)+1,MEMF_PUBLIC)) == NULL)
  142.     { Printf("StartSM : No memory for File name buffer\n");
  143.       cleanexit(20);
  144.     }
  145.     strcpy(logfile, syslogfile);
  146.     if ((logwindow = AllocVec(strlen(syslogwindow)+1,MEMF_PUBLIC)) == NULL)
  147.     { Printf("StartSM : No memory for Window name buffer\n");
  148.       FreeVec(logfile);
  149.       cleanexit(20);
  150.     }
  151.     strcpy(logwindow, syslogwindow);
  152.  
  153.     if (error = ReadConfig((STRPTR)opts[OPT_CONFIG]))
  154.     { Printf("StartSM : Error reading configuration file (%s).\n", errmsg[error]);
  155.       cleanexit(20);
  156.     }
  157.  
  158.     if (idleled) SysmonBase->sb_Flags |= SBFF_IDLELED;
  159.     SysmonBase->sb_SyslogFile = logfile;
  160.     SysmonBase->sb_SyslogWindow = logwindow;
  161.     SysmonBase->sb_FilePri = filepri;
  162.     SysmonBase->sb_WindowPri = windowpri;
  163.     SysmonBase->sb_ConsolePri = consolepri;
  164.     SysmonBase->sb_NumLogBuffers = numbuffers;
  165.     SysmonBase->sb_StampPeriod = stampperiod * 60;
  166.     if (CreateNewProcTags(NP_Entry,    SysmonBase->sb_ServerEntry,
  167.               NP_StackSize,    4000,
  168.               NP_Name,    SysmonBase->sb_ServerName,
  169.               NP_Priority,    3,
  170.               NP_WindowPtr,    -1,
  171.               TAG_DONE     ) == NULL)
  172.     { Printf("StartSM : Could not start %s process.\n", SysmonBase->sb_ServerName);
  173.       FreeVec(logfile);
  174.       FreeVec(logwindow);
  175.       cleanexit(20);
  176.     }
  177.     if ((error = SysmonBase->sb_LastGuru) != -1)
  178.     { SysLog(LOG_WARN | LOG_NOWIN, "StartSM : System rebooted by Alert !\nGuru Meditation #%08lx.%08lx\n",
  179.         error, SysBase->LastAlert[1]);
  180.     }
  181.     smSleep(20);
  182.   }
  183.   cleanexit(0);
  184. }
  185.  
  186. static void cleanexit(ULONG rc)
  187. {
  188.   if (myrda) FreeArgs(myrda);
  189.   if (SysmonBase) CloseLibrary((struct Library *)SysmonBase);
  190.   if (DOSBase) CloseLibrary((struct Library *)DOSBase);
  191.   if (wbmsg)
  192.   { Forbid();
  193.     ReplyMsg((struct Message *)wbmsg);
  194.   }
  195.   Exit(rc);
  196. }
  197.  
  198. static void KPrintf(STRPTR fmt, ...)
  199. { smVKPrintf(fmt, &fmt + 1);
  200. }
  201.  
  202. static BOOL SysLog(ULONG priority, STRPTR format, ...)
  203. { return smVSysLog(priority, format, &format + 1);
  204. }
  205.  
  206. #define    MAX_T_LINE    80
  207.  
  208. static int ReadConfig(STRPTR file)
  209. { char line[MAX_T_LINE+1];
  210.   BPTR f_in;
  211.   int error;
  212.  
  213.   if ((f_in = Open(file, MODE_OLDFILE)) == NULL) return ERR_OPEN;    
  214.   while (FGets(f_in, (STRPTR)line, MAX_T_LINE))
  215.   { if (error = InitPrefLine(line, Prefs, NB_PREFS)) return error;
  216.   }
  217.   Close(f_in);
  218.   return OK;
  219. }
  220.  
  221. static char *strtokg(char *s1, char *s2)
  222. { char *begin, *end;
  223.   static char *save = "";
  224.  
  225.   begin = s1 ? s1 : save;
  226.   if (begin == NULL) return NULL;
  227.   begin += strspn(begin, s2);
  228.   if (*begin == '\0')
  229.   { save = "";
  230.     return NULL;
  231.   }
  232.   else
  233.   { if (*begin == '\"')
  234.     { begin++;
  235.       end = strchr(begin, '\"');
  236.     }
  237.     else
  238.       end = strpbrk(begin, s2);
  239.     if (end && *end) *end++ = '\0';
  240.   }
  241.   save = end;
  242.  
  243.   return begin;
  244. }
  245.  
  246. static int InitPrefLine(char *line, Pref *pref_array, int nb_prefs)
  247. { char *p, *parm, **pflag;
  248.   char *seps = " =\t\r";
  249.   int i, l;
  250.  
  251.   if (p = strchr(line, '\n')) *p = '\0';
  252.   if ((p = strtok(line, seps)) == NULL) return OK;
  253.  
  254.   for (i=0; i < nb_prefs; i++)
  255.   { l = strlen(p);
  256.     if (l < pref_array[i].nbmin_char) continue;
  257.     if (strnicmp(p, pref_array[i].name, l) == 0) break;
  258.   }
  259.  
  260.   if (i == nb_prefs) return ERR_UNKNPREF;
  261.  
  262.   if ((parm = strtok(NULL, seps)) == NULL) return OK; /* no value */
  263.  
  264.   if (pref_array[i].type ==  P_IGNORE) return OK;
  265.  
  266.   if (pref_array[i].type == P_BOOL)
  267.   { *parm = toupper((int)*parm);
  268.     if (*parm == 'N' || *parm == 'F') *(int *)pref_array[i].pflag = 0;
  269.     if (*parm == 'Y' || *parm == 'O' || *parm == 'T') *(int *)pref_array[i].pflag = 1;
  270.   }
  271.   else if (pref_array[i].type == P_STRING)
  272.   { pflag = (char **)pref_array[i].pflag;
  273.     if (*pflag) FreeVec(*pflag);        /* Free default string buffer    */
  274.     *pflag = AllocVec(strlen(parm)+1, MEMF_PUBLIC);
  275.     if (*pflag) strcpy(*pflag, parm);
  276.     else return ERR_NOMEM;
  277.   }
  278.   else if (pref_array[i].type == P_INT)
  279.   { *(int *)pref_array[i].pflag = atoi(parm);
  280.   }
  281.  
  282.   return OK;
  283. }
  284.